﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;

namespace ProjectBase.Mvc
{
    public abstract class TextPropertyControl<TSourceProperty, TPropertyControl> : PropertyControl<TSourceProperty, TPropertyControl>
        where TPropertyControl : TextPropertyControl<TSourceProperty, TPropertyControl>
    {
        #region members
        object _textValue;
        #endregion members

        #region constructor
        protected TextPropertyControl(bool? asReadonly = null) : base(asReadonly) { }
        protected TextPropertyControl(string prefix) : base(prefix) { }
        #endregion constructor

        #region OnSourceProperty
        protected override void OnSourcePropertyChanged(string propertyName, TSourceProperty value)
        {
            _textValue = value;
            if (Type.IsValueType)
            {
                SetCssClassName(Constants.Align.Right);
            }
        }
        #endregion OnSourceProperty

        #region public Set()
        public virtual TPropertyControl SetText(string value)
        {
            _textValue = value;
            return this as TPropertyControl;
        }
        #endregion public Set()

        #region properties
        public virtual string Text
        {
            get
            {
                var result = _textValue as string;
                if (result.IsNull()
                    && _textValue.Is())
                {
                    result = _textValue.ToDisplay(Format);
                }
                return result;
            }
            set { _textValue = value; }
        }
        #endregion properties
    }
}